<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/alllayui/layui/css/layui.css}" media="all">
    <script th:src="@{/alllayui/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{/system/js/jquery.min.js}" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>修改用户</legend>
</fieldset>

<form class="layui-form" action="">
    <input type="hidden" name="id" th:value='${user.id}'>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-inline">
                <input type="text" name='userName' th:value='${user.userName}' lay-verify="required"
                       lay-reqtext="名称为必填项" placeholder="请输入名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">真实姓名</label>
            <div class="layui-input-inline">
                <input type="text" name='phoneNo' th:value='${user.phoneNo}' lay-verify="required"
                       lay-reqtext="真实姓名为必填项" placeholder="请输入真实姓名" autocomplete="off" class="layui-input">
            </div>
        </div>

    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-inline">
                <select name='sex' lay-verify="required" lay-reqtext="性别为必填项">
                    <option value="">请选择</option>
                    <option th:selected="${user.sex==0 }" value="0">女</option>
                    <option th:selected="${user.sex==1 }" value="1">男</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-inline">
                <input type="text" name='mail' th:value='${user.mail}' lay-verify="required" lay-reqtext="年龄为必填项"
                       placeholder="请输入年龄" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <select name='enable' lay-verify="required">
                    <option value="">请选择</option>
                    <option value="0" th:selected='${ user.enable == 0 }'>启用</option>
                    <option value="1" th:selected='${ user.enable == 1 }'>禁用</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">添加时间</label>
            <div class="layui-input-inline">
                <input type="text" name='addTime' th:value='${user.addTime}' lay-verify="required"
                       lay-reqtext="添加时间为必填项" placeholder="请输入添加时间" autocomplete="off" class="layui-input datePicker">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">部门ID</label>
            <div class="layui-input-inline">
                <input type="text" name='deptId' fkAttr='user_dept_fk' th:value='${user.deptId}' lay-verify="required"
                       lay-reqtext="部门ID为必填项" placeholder="请输入部门ID" autocomplete="off" class="layui-input foreignKey">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline">
                <input type="text" name='deptName' readonly="readonly" th:value='${user.deptName}' lay-verify="required"
                       lay-reqtext="部门名称为必填项" placeholder="请输入部门名称" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">简介</label>
        <div class="layui-input-block">
            <textarea name="info" placeholder="请输入简介" th:text='${user.info}' class="layui-textarea"
                      autocomplete="off"></textarea>
        </div>
    </div>

    <div class="layui-form-item">

        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="subBtn">提交</button>
        </div>

    </div>
</form>
<script>
    layui.use(['jquery', 'form', 'laydate'], function () {
        var thisIndex = parent.layer.getFrameIndex(window.name);
        var form = layui.form
            , layer = layui.layer
            , $ = layui.$;

        var laydate = layui.laydate;

        //同时绑定多个
        lay('.datePicker').each(function () {
            laydate.render({
                elem: this
                , trigger: 'click'
            });
        });

        //同时绑定多个
        $(document).on('click', '.foreignKey', function () {
            let fkAttr = $(this).attr("fkAttr");
            //iframe层-父子操作
            if (fkAttr != "") {
                layer.open({
                    type: 2,
                    area: ['700px', '450px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: '/user/choosePage?fkName=' + fkAttr
                });
            }
        });

        var parentLayer = parent.layer;
        var pWindow = parent;
        form.on('submit(subBtn)', function (data) {
            let data2Send = data.field;
            $.ajax({
                type: "post",
                url: "/user/update",
                data: data2Send,
                async: false,
                dataType: "json",
                success: function(res){
                    var msg = "";
                    console.log(res.message);
                    if (res.message === 'SUCCESS') {
                        msg = '修改成功!窗口关闭';
                    } else {
                        msg = '修改失败!窗口关闭'
                    }
                    parentLayer.msg(msg, {
                        icon: 1,
                        time: 1000
                    }, function () {
                        parentLayer.close(thisIndex);
                        pWindow.document.getElementById('searchBtn').click();
                    });
                }
            });
        });
    });

    function coverValue(coverValueColumn, chooseInfo) {
        //console.log("父页面");
        //console.log(coverValueColumn);
        //console.log(chooseInfo);
        let parse = JSON.parse(coverValueColumn);
        var valObj;
        if (chooseInfo) {
            valObj = chooseInfo[0];
        }
        //console.log(valObj);
        for (let i = 0; i < parse.length; i++) {
            var coverObj = parse[i];
            setFormValue(coverObj.mTable, valObj[coverObj.rSql]);
        }
    }

    function setFormValue(componentId, val) {
        document.getElementsByName(componentId)[0].value = val;
    }

</script>

</body>